package com.dtease.agent.sync.job;

import com.dtease.agent.sync.MetadataSyncService;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
* @description 元数据同步定时任务
* @author chentl
* @class MetadataSyncTask
* @package com.dtease.agent.metadata.sync.job
* @since 2025/5/7 14:22
*/
@Component
@RequiredArgsConstructor
public class MetadataSyncTaskJob implements CommandLineRunner {

    private final MetadataSyncService metadataSyncService;
    private final ExecutorService executorService = Executors.newFixedThreadPool(2);


    @Scheduled(cron = "0 0 2 * * ?")
    public void syncMetadata() {
        executorService.submit(metadataSyncService::syncMetadata);
    }

    @Scheduled(cron = "0 0 3 * * ?")
    public void syncMetaTags() {
        executorService.submit(metadataSyncService::syncMetaTags);
    }

    @Override
    public void run(String... args) {
//        executorService.submit(metadataSyncService::syncMetadata);
//
//        // 2s 后异步执行元数据标签同步
//        ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor();
//        scheduledExecutor.schedule(() ->
//                        executorService.submit(metadataSyncService::syncMetaTags),
//                2, TimeUnit.SECONDS
//        );
//        scheduledExecutor.shutdown();

    }
}